home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 3 / CD ACTUAL 3.iso / linux / incoming / jstools-.6v3 / jstools- / jstools-tk3.6v3.0 / lib / jprompts.tcl < prev    next >
Encoding:
Text File  |  1995-02-09  |  10.3 KB  |  359 lines

  1. # jprompts.tcl - various panels to ask the user for something
  2. # Copyright 1992-1994 by Jay Sekora.  All rights reserved, except 
  3. # that this file may be freely redistributed in whole or in part 
  4. # for non-profit, noncommercial use.
  5.  
  6. ### TO DO
  7. ###   make a real j:prompt_font
  8. ###   eval tcl commands at the global level
  9. ###   checkbox to display output of unix or tcl command
  10.  
  11. ######################################################################
  12. # global variables:
  13. #
  14. global J_PREFS env
  15. if {! [info exists J_PREFS(autoposition)]} {set J_PREFS(autoposition) 0}
  16. if {! [info exists J_PREFS(confirm)]} {set J_PREFS(confirm) 1}
  17. #
  18. ######################################################################
  19.  
  20.  
  21. ######################################################################
  22. # j:prompt ?options? - prompt the user for information
  23. # options are:
  24. #   -text (default "Enter a value:"
  25. #   -default (default "")
  26. #   -cancelvalue (default "")
  27. #   -file (default 0)
  28. #   -title (default "Prompt")
  29. # if $file, then the Tab key will do filename completion
  30. ######################################################################
  31.  
  32. proc j:prompt { args } {
  33.   j:parse_args {
  34.     {text "Enter a value:"}
  35.     {default ""}
  36.     {cancelvalue ""}
  37.     {file 0}
  38.     {title Prompt}
  39.   }
  40.   
  41.   global j_prompt
  42.  
  43.   set old_focus [j:current_focus]    ;# so we can restore original focus
  44.  
  45.   toplevel .pr
  46.   wm title .pr $title
  47.   
  48.   message .pr.msg -width 300 -anchor w -text $text
  49.   entry .pr.field -relief sunken -width 40
  50.   j:buttonbar .pr.b -default ok -buttons [format {
  51.     {ok OK {set j_prompt(result) [.pr.field get]; destroy .pr}}
  52.     {cancel Cancel {set j_prompt(result) {%s}; destroy .pr}}
  53.   } $cancelvalue]
  54.  
  55.   pack .pr.msg -side top -fill both -expand yes -padx 10
  56.   pack .pr.field -side top -padx 10 -pady 10
  57.   pack .pr.b -side bottom -fill x
  58.   pack [j:rule .pr -width 200] -side bottom -fill x
  59.  
  60.   .pr.field delete 0 end
  61.   .pr.field insert end $default
  62.   
  63.   j:dialogue .pr            ;# position in centre of screen
  64.  
  65.   if $file {
  66.     bind .pr.field <Tab> {
  67.       set f [%W get]
  68.       %W delete 0 end
  69.       %W insert end [j:expand_filename $f]
  70.     }
  71.   }
  72.   j:default_button .pr.b.ok .pr.field
  73.   j:cancel_button .pr.b.cancel .pr.field
  74.  
  75.   focus .pr.field
  76.   grab .pr
  77.   tkwait window .pr
  78.   focus $old_focus
  79.   return $j_prompt(result)
  80. }
  81.  
  82. ######################################################################
  83. # j:prompt_font ?options? - prompt for a font (via xfontsel)
  84. # options are:
  85. #   -prompt (default "Font:", but currently ignored)
  86. #   -pattern (default "*")
  87. # usage of xfontsel (`quit' button) not obvious!
  88. ######################################################################
  89.  
  90. proc j:prompt_font { args } {
  91.   j:parse_args {
  92.     {prompt "Font:"}
  93.     {pattern "*"}
  94.   }
  95.   return [exec xfontsel -pattern $pattern -print]
  96. }
  97.  
  98. ######################################################################
  99. # j:prompt_tcl - prompt for a tcl command and execute it
  100. ######################################################################
  101.  
  102. proc j:prompt_tcl {} {
  103.   global j_prompt_tcl
  104.   append j_prompt_tcl(RESULT) {}
  105.  
  106.   set prompt_result [j:prompt \
  107.     -text "Tcl Command:" -default $j_prompt_tcl(RESULT)]
  108.   if {$prompt_result != {}} then {
  109.     set j_prompt_tcl(RESULT) $prompt_result
  110.     set result [uplevel #0 $j_prompt_tcl(RESULT)]
  111.     set length [string length $result]
  112.     if {$length == 0} {
  113.       return
  114.     }
  115.     if {$length < 40 && ! [string match "*\[\t\r\]*" $result]} {
  116.       j:alert -title "Tcl result" -text $result
  117.       return
  118.     } else {
  119.       j:more -title "Result of Tcl command" -text $result
  120.       return
  121.     }
  122.   }
  123. }
  124.  
  125. ######################################################################
  126. # j:prompt_unix - prompt for a unix command and execute it
  127. ######################################################################
  128.  
  129. proc j:prompt_unix {} {
  130.   global j_prompt_unix
  131.   append j_prompt_unix(RESULT) {}
  132.  
  133.   set prompt_result [j:prompt \
  134.     -text "Unix Command:" -default $j_prompt_unix(RESULT)]
  135.   if {$prompt_result != {}} then {
  136.     set j_prompt_unix(RESULT) $prompt_result
  137.     set command $prompt_result
  138.     set result [uplevel #0 exec $command < /dev/null]
  139.     set length [string length $result]
  140.     if {$length == 0} {
  141.       j:alert -text "No output from $command."
  142.       return
  143.     }
  144.     if {$length < 40 && ! [string match "*\[\t\r\]*" $result]} {
  145.       j:alert -title "Command output" -text $result
  146.       return
  147.     } else {
  148.       j:more -title "Output of $command" -text $result
  149.       return
  150.     }
  151.   }
  152. }
  153.  
  154. ######################################################################
  155. # j:prompt_colour_name - prompt for a colour name
  156. ######################################################################
  157. ### PROBLEM: LOCATION OF /usr/lib/X11/rgb.txt IS HARDCODED!
  158. ### Also, should open and process without forking off an awk
  159. ### Also, getting $w into strings is done in an ugly manner
  160.  
  161. proc j:prompt_colour_name { args } {
  162.   j:parse_args {
  163.     {prompt "Choose a colour:"}
  164.     {title "Colour Name Selector"}
  165.   }
  166.   global j_prompt
  167.   global J_PREFS
  168.   if {[lsearch [array names J_PREFS] {scrollbarside}] == -1} {
  169.     set J_PREFS(scrollbarside) right ;# make sure it's defined
  170.   }
  171.   
  172. ###   if {[info exists j_prompt(count)]} then {
  173. ###     set j_prompt(count) [expr {$j_prompt(count) + 1}]
  174. ###   } else {
  175. ###     set j_prompt(count) 0
  176. ###   }
  177. ### 
  178. ###   set w ".prompt$j_prompt(count)"
  179.  
  180.   set w ".prompt_colour_name"
  181.   toplevel $w
  182.   wm title $w $title
  183.   
  184.   set rgbfile /usr/lib/X11/rgb.txt
  185.   if [file isfile $rgbfile] {
  186.     set colourlist [lsort [exec awk { NF == 4 { print $4 } } $rgbfile]]
  187.   } else {
  188.     set colourlist {
  189.       aquamarine bisque black blue brown burlywood coral crimson cyan
  190.       firebrick gold goldenrod green grey grey25 grey33 grey50 grey66
  191.       grey75 khaki lavender magenta maroon navy orange orchid pink plum
  192.       purple red salmon tan tomato turquoise white yellow
  193.     }
  194.   }
  195.   
  196.   set j_prompt(colour) {}
  197.   
  198.   label $w.l -text $prompt
  199.   j:buttonbar $w.b -default ok -orient vertical -buttons [list \
  200.     [list ok OK [format {
  201.         catch {set j_prompt(colour) [%s.list get [%s.list curselection]]}
  202.         destroy %s
  203.       } $w $w $w] \
  204.     ] \
  205.   ]
  206.   frame $w.frame -width 100 -height 100 \
  207.     -background bisque -relief raised -borderwidth 2
  208.   frame $w.list
  209.   scrollbar $w.list.sb -relief flat -command "$w.list.lb yview"
  210.   listbox $w.list.lb -yscroll "$w.list.sb set" -relief flat -setgrid true
  211.   ####### following supports both tk3.6 and 4.0:
  212.   if [catch {$w.list.lb configure -geometry 20x20}] {
  213.     $w.list.lb configure -width 20 -height 20
  214.   }
  215.   
  216.   pack $w.list.sb [j:rule $w.list] \
  217.     -side $J_PREFS(scrollbarside) -fill y
  218.   pack $w.list.lb -in $w.list -side left -expand yes -fill both
  219.   
  220.   pack $w.l [j:rule $w] -side top -fill x
  221.   pack $w.list [j:rule $w] -side left -expand yes -fill both
  222.   pack $w.frame -side top -fill both -expand yes -padx 10 -pady 10
  223.   pack $w.b -side bottom -fill x
  224.   pack [j:rule $w] -side bottom -fill x
  225.   
  226.   # Fill the listbox with a list of several useful colours:
  227.   
  228.   foreach i $colourlist {
  229.     $w.list.lb insert end $i
  230.   }
  231.   
  232.   # Set up bindings for the browser.
  233.   
  234.   bind $w.list.lb <Control-q> "destroy $w"
  235.   bind $w.list.lb <Control-c> "destroy $w"
  236.   focus $w.list.lb
  237.   bind $w.list.lb <Button-1> "
  238.     $w.list.lb select from \[$w.list.lb nearest %y\]
  239.     catch {set j_prompt(colour) \[$w.list.lb get \[$w.list.lb curselection\]\]}
  240.     $w.frame config -background \$j_prompt(colour)
  241.   "
  242.   bind $w.list.lb <Double-Button-1> "
  243.     $w.b.ok invoke
  244.   "
  245.   
  246.   j:default_button $w.b.ok $w
  247.   focus $w
  248.   j:dialogue $w
  249.   tkwait window $w
  250.   if {$j_prompt(colour) == ""} {set j_prompt(colour) bisque}
  251.   return $j_prompt(colour)
  252. }
  253.  
  254. proc j:prompt_color_name \
  255.   [info args j:prompt_colour_name] \
  256.   [info body j:prompt_colour_name]
  257.  
  258. ######################################################################
  259. # j:prompt_colour_rgb - prompt for a colour RGB value
  260. #   An eviscerated version of selcol.tcl by Sam Shen <sls@aero.org>,
  261. #   which also let you choose HSV values
  262. ######################################################################
  263.  
  264. proc j:prompt_colour_rgb { args } {
  265.   j:parse_args {
  266.     {prompt "Choose a colour:"}
  267.     {title "RGB Colour Selector"}
  268.   }
  269.   global j_prompt
  270.   
  271.   set j_prompt(red) 255
  272.   set j_prompt(blue) 196
  273.   set j_prompt(green) 228
  274.   set j_prompt(flag) 0
  275.   
  276.   set w .prompt_rgb
  277.   toplevel $w
  278.   wm title $w $title
  279.   wm minsize $w 100 100
  280.   wm maxsize $w [winfo screenwidth $w] [winfo screenheight $w]
  281.   
  282.   label $w.l -text $prompt
  283.  
  284.   frame $w.patch -width 100 -height 100 -relief raised -borderwidth 2
  285.   entry $w.value -width 12
  286.   bind $w.value <1> {%W select from @0; %W select to end}
  287.   
  288.   frame $w.scales
  289.   set j_prompt(flag) 1
  290.   j:prompt_colour_rgb:make_scale $w $w.scales.red red 255 Red
  291.   j:prompt_colour_rgb:make_scale $w $w.scales.green green 255 Green
  292.   j:prompt_colour_rgb:make_scale $w $w.scales.blue blue 255 Blue
  293.   pack $w.scales.red $w.scales.green $w.scales.blue \
  294.     -side left -fill y -expand yes
  295.   set j_prompt(flag) 0
  296.   
  297.   j:buttonbar $w.b -default ok -orient vertical -buttons {
  298.     {
  299.       ok OK { }
  300.     }
  301.   }
  302.   
  303.   $w.b.ok configure -command "
  304.     set j_prompt(return) \[$w.value get\]
  305.     destroy $w
  306.   "
  307.   
  308.   pack $w.l
  309.   pack [j:rule $w] -fill x
  310.   pack $w.scales -expand yes -side left -fill y
  311.   pack [j:rule $w] -side left -fill y
  312.   pack $w.value -fill both
  313.   pack $w.patch -expand yes -fill both -padx 10 -pady 10
  314.   pack [j:rule $w] -fill x
  315.   pack $w.b -fill x
  316.   
  317.   j:prompt_colour_rgb:update_colour $w red $j_prompt(red)
  318.   
  319.   j:default_button $w.b.ok $w
  320.   focus $w
  321.   j:dialogue $w
  322.   tkwait window $w
  323.   return $j_prompt(return)
  324. }
  325.  
  326. proc j:prompt_colour_rgb:make_scale {w name var to title} {
  327.   global j_prompt
  328.   
  329.   frame $name
  330.   scale $name.scale -to $to \
  331.     -command "j:prompt_colour_rgb:update_colour $w $var"
  332.   $name.scale set [set j_prompt($var)]
  333.   label $name.label -text $title
  334.   pack $name.label -in $name
  335.   pack $name.scale -in $name -expand yes -fill y
  336. }
  337.  
  338. proc j:prompt_colour_rgb:update_colour {w var value} {
  339.   global j_prompt
  340.  
  341.   if {$j_prompt(flag) == 1} {return}
  342.   set j_prompt(flag) 1
  343.   set j_prompt($var) $value
  344.   set colour [format "#%02x%02x%02x" \
  345.     $j_prompt(red) $j_prompt(green) $j_prompt(blue)]
  346.   catch {}
  347.   $w.patch configure -background $colour
  348.   $w.value delete @0 end
  349.   $w.value insert 0 $colour
  350.   set j_prompt(flag) 0
  351. }
  352.  
  353. ######################################################################
  354.  
  355. proc j:prompt_color_rgb \
  356.   [info args j:prompt_colour_rgb] \
  357.   [info body j:prompt_colour_rgb]
  358.